<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="com.googlecode.contests_repo.core.users.AccessRight"%>
<%@page import="com.googlecode.contests_repo.core.users.UserManager"%>
<%@page import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="com.googlecode.contests_repo.core.model.ResourcesUtils"%>
<%@page import="com.googlecode.contests_repo.core.model.Series"%>
<%@page import="com.googlecode.contests_repo.core.model.Resource"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.io.File"%>

	<h1>List of all available series</h1>
	<%
		String username = (String) session.getAttribute("username");
		String path = request.getParameter("path");
		boolean canEdit = "1".equals(request.getParameter("edit"));
		String repoRoot = ConfigurationManager.getInstance().getRepoRoot();
		String resourcePath = repoRoot + File.separator + path;
		boolean canDelete = UserManager.hasAccessRight(username,
				resourcePath, AccessRight.VIEW_FULL);
	%>
	<table>
		<tr>
			<th>Id</th>
			<th>Title</th>
			<th>About</th>
			<th>View</th>
			<th>Download</th>
			<% if (canEdit) { %>
			<th>Edit</th>
			<% } %>
			<% if (canEdit && canDelete) { %>
			<th>Delete</th>
			<% } %>
		</tr>
		<%
			String seriesPath = ConfigurationManager.getInstance().getSeriesPath();
			String seriesRelativePath = ConfigurationManager.getInstance().getSeriesRelativePath();
			List<Series> seriesList = ResourcesUtils.fetchResources(new File(seriesPath));
			if (seriesList != null) {
				for (Series series : seriesList) {
					String seriesDir = seriesPath + File.separator + series.getId();
					if (UserManager.hasAccessRight(username, seriesDir, AccessRight.LIST)) {
		%>
		<tr>
			<td><%=series.getId()%></td>
			<td><%=series.getTitle()%></td>
		<%
					}
					if (UserManager.hasAccessRight(username, seriesDir, AccessRight.VIEW)) {
		%>
			<td><%=series.getAbout()%></td>
			<td><a href="resource.jsp?path=<%=seriesRelativePath + File.separator + series.getId()%>">details</a></td>
			<%
					}
					if (UserManager.hasAccessRight(username, seriesDir, AccessRight.VIEW)) {
			%>
			<td><a href="fetchZipped?series=<%=series.getId()%>">download</a></td>
			<% 
					}
					if (canEdit && UserManager.hasAccessRight(username, seriesDir, AccessRight.VIEW_FULL)) {
			%>
			<td><a href="resource.jsp?path=<%=seriesRelativePath + File.separator + series.getId()%>&amp;edit=1">edit</a></td>
			<%
					}
					if (canEdit && canDelete) {
			%>
			<td><a href="delete_file?path=<%=seriesRelativePath + File.separator + series.getId()%>">delete</a></td>
		<%
					} %>
		</tr>
		<%
				}
			}
		%>
	</table>